<?php
class TypesController extends AppController {

	var $name = 'Types';

	function index() {
		$this->Type->recursive = 0;
		$this->set('types', $this->paginate());
	}

	function view($id = null) {
		$this->layout = 'trangchu';
		$this->set('catalog', $this->Catalog->read(null, $id));
		$data = $this->Post->find('all', array(
											'conditions' => array('type_id' => $id, 'Post.vip' => 1),
											'order' => array('Post.created' => 'DESC'),
		));
		
		$this->set('data', $data);
		//set bien id
		$this->set('id',$id);
		//lay so tin hien thi tren mot trang
		$display=$this->requestAction(array("controller"=>"posts","action"=>"tinvip"));		
		//dem so tin
		$dem = $this->Post->find('count', array(
											'conditions' => array('catalog_id' => $id, 'Post.vip' => 0),											
		));
		//thiet lap gia tri cho bien p
		if(isset($_GET['p']) && (int)$_GET['p']) {
                $p = $_GET['p'];
		}else{
			$p=1;
		}
		$this->set('p', $p);
		//xac dinh so trang can hien thi
		if($dem > $display) {
			$page = ceil($dem/$display);
		} else {
			$page = 1;
		}
		$this->set('page', $page);
		//lay du lieu tin thuong
		$data2 = $this->Post->find('all', array(
											'conditions' => array('type_id' => $id, 'Post.vip' => 0),
											'order' => array('Post.created' => 'DESC'),
											'limit' =>$display,
											'page' => $p,
		));
		$this->set('data2', $data2);
		//if (!$id) {
			//$this->Session->setFlash(__('Invalid type', true));
			//$this->redirect(array('action' => 'index'));
		//}
		$this->set('type', $this->Type->read(null, $id));
	}

	function add() {
		$this->layout ='admin_quanli';
		if (!empty($this->data)) {
			$this->Type->create();
			if ($this->Type->save($this->data)) {
				$this->Session->setFlash(__('The type has been saved', true));
				$this->redirect('/');
			} else {
				$this->Session->setFlash(__('The type could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid type', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Type->save($this->data)) {
				$this->Session->setFlash(__('The type has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The type could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Type->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for type', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Type->delete($id)) {
			$this->Session->setFlash(__('Type deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Type was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function select(){
		$this->layout='trangchu';
		$datatype=$this->Type->find("all");
		return $datatype;
	}
}
